/*
 * Created on Mar 3, 2010
 */
package ro.letsdoitromania;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Vector;
import javax.microedition.rms.RecordStore;
import com.mgmaps.app.APPSettings;
import com.mgmaps.utils.ExtLogger;

public class LDIRSettings extends APPSettings {

  public static final String UPDATED = "Apr. 27, 2010";
  
  // GPS is valid for 1 minute
  public static final int GPS_VALIDITY_TIME = 60000;
  
  // log command
  public static final boolean LOG_AVAILABLE = true; 
  
  // API URLs
  public static final String AUTH_URL = "http://ldir-mobile.gtstelecom.ro/LetsDoItWS/AuthService.svc/v1/usersalt"; 
  public static final String UPLOAD_URL = "http://ldir-mobile.gtstelecom.ro/LetsDoItWS/WastesService.svc/v1/wastes"; 
  
  public static Vector points = new Vector();
  
  public static String username = "";
  public static String password = "";
  public static String usersalt = "";
  
  public static final boolean isTest = false;
  
  /**
   * Set app constants.
   */
  private static void setConstants() {
    
    // platform handlers << please change in APPSettings as the obfuscator is fooled otherwise
    // platformHandlerClassBB = "com.mgmaps.platform.PlatformBB";
    // platformHandlerClassNokia = "com.mgmaps.platform.PlatformNokia";
    
    // application name, used for RS
    rsName = "LDIR";
    // application URL
    appURL = "http://www.letsdoitromania.ro";
    // application version if not detected from jad
    version = "1.0.11";
    // current props version
    propsVersion = 4;
    // default language = ro
    language = "ro_ro";
    
    // features
    featureMap = false;
    featureGPS = true;
    featureOnline = true;
    featureBacklight = false;
  }
  
  public static void initialize() {
    setConstants();
    
    // super.initialize
    APPSettings.initialize();
  }
  
  /**
   * Load settings.
   */
  public static void load() {
    final RecordStore rs = openRecordStore();
    
    byte[] data = null;
    DataInputStream dis = null;
    
    try {
      APPSettings.load(rs);
      
      // load specific settings
      try {
        // points
        data = rs.getRecord(3);
        dis = new DataInputStream(new ByteArrayInputStream(data));
        int numPoints = dis.readInt();
        points = new Vector();
        for (int i=0; i<numPoints; i++) {
          TrashPointData point = null;
          try {
            point = new TrashPointData(points.size()+1, dis);
          }
          catch (Exception ex3) {
            ExtLogger.handle(ex3, false, "Error loading point data, ignoring.");
          }
          points.addElement(point);
        }
      }
      catch (Exception ex2) {
        ExtLogger.handle(ex2, false, "Error loading points");
        points = new Vector();
      }

      try {
        // login, etc.
        data = rs.getRecord(4);
        dis = new DataInputStream(new ByteArrayInputStream(data));
        username = dis.readUTF();
        password = dis.readUTF();
        usersalt = "";
      }
      catch (Exception ex2) {
        ExtLogger.handle(ex2, false, "Error loading login settings");
        username = "";
        password = "";
        usersalt = "";
      } 
    }
    catch(Exception ex) {
      ExtLogger.handle(ex, false, "Error loading settings");
    }

    closeRecordStore(rs);
  }
  
  /**
   * Save settings.
   */
  public static void save() {
    emptyRecordStore();
    final RecordStore rs = openRecordStore();
    
    ByteArrayOutputStream bos = null;
    DataOutputStream dos = null;
    try {
      APPSettings.save(rs);

      bos = new ByteArrayOutputStream();
      dos = new DataOutputStream(bos);
      
      // save specific settings
      int numPoints = points.size();
      dos.writeInt(numPoints);
      for (int i=0; i<numPoints; i++)
        ((TrashPointData) points.elementAt(i)).write(dos);
      writeRSEntry(rs, dos, bos, 3);
      
      dos.writeUTF(username);
      dos.writeUTF(password);
      writeRSEntry(rs, dos, bos, 4);
    }
    catch (Exception ex) {
      ExtLogger.handle(ex, false, "Error saving settings");
    }
    
    closeRecordStore(rs);
  }

  public static void updatePointIDs() {
    int sz = points.size();
    for (int i=0; i<sz; i++) {
      TrashPointData p = (TrashPointData) points.elementAt(i);
      p.setID(i+1);
    }
  }
  
}
